<?php
	
namespace App\Http\Controllers;

use DB;
use Laravel\Lumen\Routing\Controller;
use Illuminate\Http\Request;

class ConsumerCouponController extends BaseController{

    public function index(Request $request,$id){
        $data = $request->all();
        $couponResult = DB::table('consumer_coupon')
                        ->join('coupon','consumer_coupon.coupon_id','=','coupon.id')
                        ->join('seller','coupon.seller_id','=','seller.id')
                        ->select('coupon.id','coupon.subject','seller.id as seller_id',
                                 'seller.name as seller_name','coupon.img_dir',
                                 'coupon.content','coupon.start_time','coupon.end_time')
                        ->where('consumer_coupon.consumer_id',$id);
        $time = date("Y-m-d");
        if(isset($data['condition'])){
            switch ($data['condition']){
                case '1':
                    $couponResult = $couponResult->where('coupon.end_time','<',$time);
                    break;
                
                case '2':
                    $couponResult = $couponResult->where('coupon.end_time','>=',$time)->where('consumer_coupon.is_used','=',0);
                    break;

                case '3':
                    $couponResult = $couponResult->where('consumer_coupon.is_used','=',1);
                    break;    
            }        
        }
        if(isset($data['seller_id'])){
            $couponResult = $couponResult->where('seller.id',$data['seller_id']);
        }
        $couponResult = $couponResult
                        ->where('consumer_coupon.is_delete',0)
                        ->orderBy('consumer_coupon.created_at','DESC')
                        ->paginate(8)
                        ->toJson();
        $couponResult = json_decode($couponResult);
        return $this::jsonResponse(false,$couponResult);

    }

    public function show($id,$coupon_id){

        $couponResult = DB::table('consumer_coupon')
                        ->join('coupon','consumer_coupon.coupon_id','=','coupon.id')
                        ->join('seller','coupon.seller_id','=','seller.id')
                        ->select('coupon.id','coupon.subject','seller.id as seller_id','seller.name as seller_name',
                                 'seller.address','seller.tel','coupon.img_dir',
                                 'coupon.content','coupon.start_time','coupon.end_time','consumer_coupon.is_used','consumer_coupon.coupon_url')
                        ->where('consumer_coupon.consumer_id',$id)
                        ->where('consumer_coupon.coupon_id',$coupon_id)
                        ->get();
        return $this::jsonResponse(false,$couponResult);                
    }

    public function store(Request $request,$id){
        $data = $request->all();
        $couponId = $data['couponId'];
        $couponData = DB::table('coupon')->select('*')->where('id',$couponId)->first();
        if(empty($couponData)){
            return $this::jsonResponse(true,'','优惠券不存在');
        }     
        $time = date("Y-m-d");
        $couponRecord = DB::table('consumer_coupon')
                          ->select("*")
                          ->where('consumer_id',$id)
                          ->where('coupon_id',$couponId)
                          ->first();   
        $couponCount = DB::table('consumer_coupon')->where('coupon_id',$couponId)->count();
                      
        if($couponData->end_time < $time){
            return $this::jsonResponse(true,'','优惠券已过期');
        }
        if($couponCount >= $couponData->count){
            return $this::jsonResponse(true,'','优惠券数量不足');
        }
        if($couponRecord){
            if($couponRecord->is_delete == 1){
                DB::table('consumer_coupon')->where('id',$couponRecord[0]->id)->update(array('is_delete' => 0));
                return $this::jsonResponse(false,'','领取成功'); 
            }
            return $this::jsonResponse(true,'','您已经拥有此优惠券');
            
        }
        $result = DB::table('consumer_coupon')->insertGetId(array('consumer_id' => $id,
                                                                  'coupon_id' => $couponId,
                                                                  'coupon_url'=>'http://chinalove99.net:82/user/'.$id.'/coupon/'.$couponId.'/use'));
        $count_get = DB::table('coupon')->where('id',$couponId)->update(array('count_get'=>$couponCount+1));

        if(!$result){
            return $this::jsonResponse(true,'','领取失败'); 
        }else{
            return $this::jsonResponse(false,$result,'领取成功'); 
        }               
    }

    public function userConpon(Request $request,$id,$coupon_id)
    {
        $couponResult = DB::table('consumer_coupon')
                        ->join('coupon','consumer_coupon.coupon_id','=','coupon.id')
                        ->join('seller','coupon.seller_id','=','seller.id')
                        ->join('consumer','consumer.id','=','consumer_coupon.consumer_id')
                        ->select('coupon.id','coupon.subject','seller.id as seller_id','seller.name as seller_name',
                                 'seller.address','seller.tel','coupon.img_dir','consumer.id as user_id','consumer.nickname as user_name',
                                 'coupon.content','coupon.start_time','coupon.end_time','consumer_coupon.is_used','consumer_coupon.coupon_url')
                        ->where('consumer_coupon.consumer_id',$id)
                        ->where('consumer_coupon.coupon_id',$coupon_id)
                        ->first();
        $result['coupon'] = $couponResult;
        $result['user_id'] = $id; 
        $result['coupon_id'] = $coupon_id;                  
        return view('use_coupon',['result'=>$result]);   
    }

    public function useConpon(Request $request,$id,$coupon_id)
    {
        $data = $request->all();
        $coupon = DB::table('coupon')->select('*')->where('id',$coupon_id)->first();
        $time = date('Y-m-d');
        if($coupon->start_time > $time){
            return $this::jsonResponse(true,'','该优惠券尚未到使用日期'); 
        }
        if($coupon->end_time < $time){
            return $this::jsonResponse(true,'','该优惠券已过期'); 
        }
        $result = DB::table('consumer_coupon')
                    ->where('consumer_coupon.consumer_id',$id)   
                    ->where('consumer_coupon.coupon_id',$coupon_id)
                    ->update(array('consumer_coupon.is_used'=>1));
        if(!$result){
            return $this::jsonResponse(true,'','消费失败'); 
        }else{
            return $this::jsonResponse(false,'','消费成功');   
        }                   
    }

    public function destroy($id,$coupon_id){

        $result = DB::table('consumer_coupon')
                    ->where('consumer_coupon.consumer_id',$id)   
                    ->where('consumer_coupon.coupon_id',$coupon_id)
                    ->update(array('consumer_coupon.is_delete'=>1));
        if(!$result){
            return $this::jsonResponse(true,'','删除失败'); 
        }else{
            return $this::jsonResponse(false,'','删除成功');   
        }                 
    }
}

?>